<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//引入Role模型类
use App\Http\Models\Role;
//引入permission
use App\Http\Models\Permission;
class RoleController extends Controller
{
    //角色列表
    public function index(){
        $data = Role::get();
        return view('admin.role.index',compact('data'));
    }

    //角色编辑
    public function edit(Request $request,Role $role){
        if($request->isMethod('post')){
            $permissions = $request->input('permission');

//            dd($data);
            //制作权限
            $ps_ids = implode(',',$permissions);
//            dd($ps_ids);
            //权限对应的'控制器-方法' 信息
            $ps_ca = Permission::whereIn('ps_id',$permissions)
                ->select(\DB::raw("concat(ps_c,'-',ps_a) as ca"))
                ->whereIn('ps_level',['1','2'])
                ->pluck('ca');
            //将取出来的$ps_ca  用逗号连接  入库
            $ps_ca = implode(',',$ps_ca->toArray());
//            dd($ps_ca);
            $res = $role->update([
                'ps_ids'=>$ps_ids,
                'ps_ca'    =>$ps_ca
            ]);
            if($res){
                return ['success'=>true];
            }else{
                return ['success'=>false,'errorinfo'=>'数据修改失败'];
            }
        }

        //获取所有权限
        $permission_A = Permission::where('ps_level','0')->get();
//        dd($permission_A);
        $permission_B = Permission::where('ps_level','1')->get();
//        dd($permission_A,$permission_B);
        $permission_C = Permission::where('ps_level','2')->get();
        $ps_ids = explode(',',$role->ps_ids);

        return view('admin.role.edit',compact('role','permission_A','permission_B','permission_C','ps_ids'));
    }

}
